<template>
	<div>
		<el-dialog :visible="dialogFormVisible" width="20%" @close="closeDialogForm" :modal-append-to-body="false"
		 :close-on-click-modal="false" :close-on-press-escape="false">
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="ruleForm">
				<el-form-item label="IP地址" prop="ipaddress">
					<el-input v-model="ruleForm.ipaddress"></el-input>
				</el-form-item>
				<el-form-item label="IP类型" prop="ipaddresstype">
					<el-select v-model="ruleForm.ipaddresstype">
						<el-option label="公网" value="0"></el-option>
						<el-option label="内网" value="1"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="所属机房">
					<el-select v-model="ruleForm.idcid" placeholder="请选择" >
						<el-option v-for="item in idcoptions" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				
				<el-form-item>
					<el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
				</el-form-item>
			</el-form>
		</el-dialog>
	</div>
</template>

<script>
	import {
		update,
		exist,
	} from '@/api/ipAddress';
	import {
		mapGetters
	} from 'vuex';
	export default {
		props: {
			dialogFormVisible: Boolean,
		},
		data() {
			var validatorAddress = (rule, value, callback) => {
				var reg = "^((25[0-5]|2[0-4]\\d|[1]{1}\\d{1}\\d{1}|[1-9]{1}\\d{1}|\\d{1})($|(?!\\.$)\\.)){4}$";
				if (!value.match(reg)) {
					callback(new Error('输入的IP地址不合法!'))
				}
				callback()
			};
			return {
				ruleForm: {
					ipaddress: '',
					id: '',
					ipaddresstype: "0",
				},
				rules: {
					ipaddress: [{
							required: true,
							message: '请填写IP地址',
							trigger: 'blur',
						},
						{
							validator: validatorAddress,
							trigger: 'blur',
						},
					]
				},
			}
		},
		computed: {
			...mapGetters({
				idcoptions: 'getIdcOptions',
			})
		
		},
		methods: {
			submitForm(formName) {
				console.dir(this.$refs[formName].validate)
				this.$refs[formName].validate((valid) => {
					if (valid) {
						exist(this.ruleForm)
						.then((response)=>{
							if(response.code == "200"){
								update(this.ruleForm)
									.then((response) => {
										this.$store.dispatch('setipaddresslistOptions');
										this.$emit('update:dialogFormVisible', false)
									})
							}else{
								this.$message({
									type: 'error',
									message: response.message
								});
							}
						})
						
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			closeDialogForm() {
				this.$emit('update:dialogFormVisible', false)
			}
		},
	}
</script>
<style>
</style>
